package com.qding.park.manager.payment.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.qding.park.common.utils.http.SimpleHttpParam;
import com.qding.park.common.utils.http.SimpleHttpResult;
import com.qding.park.common.utils.http.SimpleHttpUtils;
import com.qding.park.manager.payment.PaymentInterfaceServiec;
import com.qding.park.manager.payment.config.OrderUrlConfig;
import com.qding.park.manager.pojo.payment.AbstractResult;
import com.qding.park.manager.pojo.payment.OrderParameter;
import com.qding.park.manager.pojo.payment.ResultCreateorder;

@Service("paymentInterfaceServiec")
public class PaymentInterfaceServiecImpl implements PaymentInterfaceServiec{

	private static final Log _log = LogFactory.getLog(PaymentInterfaceServiecImpl.class);
	@Override
	public ResultCreateorder createOrder(OrderParameter orderparameter) {
		String url=OrderUrlConfig.getCreateOrderUrl();
		if(url.startsWith(SimpleHttpUtils.HTTPS_PREFIX)){
			SimpleHttpParam httpParam=new SimpleHttpParam(url);
			httpParam.setMethod(SimpleHttpUtils.HTTP_METHOD_POST);
			JSONObject  paramJson=new JSONObject();
			paramJson.put("projectId", orderparameter.getProjectId());
			paramJson.put("orderType", orderparameter.getOrderType());
			paramJson.put("totalPrice", orderparameter.getTotalPrice()/100.0f);
			paramJson.put("orderSource", orderparameter.getOrderSource());
			paramJson.put("szCarpassId",orderparameter.getSzCarpassId());
			paramJson.put("memberId",orderparameter.getMemberId());
			httpParam.addParameter("body", paramJson.toJSONString());
			_log.info("url:"+url);
			_log.info("paramJson:"+paramJson);
			SimpleHttpResult  result=SimpleHttpUtils.httpRequest(httpParam);
			_log.info("result:"+result.getContent());
			int code=result.getStatusCode();
			ResultCreateorder resultCreateorder=new ResultCreateorder();
			if(code!=200){
				resultCreateorder.setCode(code);
				resultCreateorder.setMessage(result.getExceptionMsg());
			}else{
				String content=result.getContent();
				
				JSONObject jsonObj=JSONObject.parseObject(content);
				int jsonCode=jsonObj.getIntValue("code");
				
				JSONObject dataJson=jsonObj.getJSONObject("data");
				String message=dataJson.getString("message");
				
				if(jsonCode==AbstractResult.ResultCodeEnum.SUCESS.getCode()){
					resultCreateorder.setCode(jsonCode);
					resultCreateorder.setMessage(message);
					JSONObject entityObj=dataJson.getJSONObject("entity");
					String orderCode=entityObj.getString("orderCode");
					double totalShouldPay=entityObj.getDouble("totalShouldPay");
					resultCreateorder.setOrderId(orderCode);
					resultCreateorder.setTotalShouldPay(totalShouldPay*100);
				}else{
					resultCreateorder.setCode(jsonCode);
					resultCreateorder.setMessage(message);
				}
			}
			
			return resultCreateorder;
		}else{
			SimpleHttpParam httpParam=new SimpleHttpParam(url);
			httpParam.setMethod(SimpleHttpUtils.HTTP_METHOD_POST);
//			httpParam.addParameter(key, value);
			//
			JSONObject  paramJson=new JSONObject();
			paramJson.put("projectId", orderparameter.getProjectId());
			paramJson.put("orderType", orderparameter.getOrderType());
			paramJson.put("totalPrice", orderparameter.getTotalPrice()/100.0f);
			paramJson.put("orderSource", orderparameter.getOrderSource());
			paramJson.put("szCarpassId",orderparameter.getSzCarpassId());
			paramJson.put("memberId",orderparameter.getMemberId());
			httpParam.addParameter("body", paramJson.toJSONString());
			_log.info("url:"+url);
			_log.info("paramJson:"+paramJson);
			SimpleHttpResult  result=SimpleHttpUtils.httpRequest(httpParam);
			_log.info("result:"+result.getContent());
			int code=result.getStatusCode();
			ResultCreateorder resultCreateorder=new ResultCreateorder();
			if(code!=200){
				resultCreateorder.setCode(code);
				resultCreateorder.setMessage(result.getExceptionMsg());
			}else{
				String content=result.getContent();
				
				JSONObject jsonObj=JSONObject.parseObject(content);
				int jsonCode=jsonObj.getIntValue("code");
				
				JSONObject dataJson=jsonObj.getJSONObject("data");
				String message=dataJson.getString("message");
				
				if(jsonCode==AbstractResult.ResultCodeEnum.SUCESS.getCode()){
					resultCreateorder.setCode(jsonCode);
					resultCreateorder.setMessage(message);
					JSONObject entityObj=dataJson.getJSONObject("entity");
					String orderCode=entityObj.getString("orderCode");
					double totalShouldPay=entityObj.getDouble("totalShouldPay");
					resultCreateorder.setOrderId(orderCode);
					resultCreateorder.setTotalShouldPay(totalShouldPay*100);
				}else{
					resultCreateorder.setCode(jsonCode);
					resultCreateorder.setMessage(message);
				}
			}
			
			return resultCreateorder;
		}
		
	}

	@Override
	public ResultCreateorder refundOrder(String carpassId) {
		String url=OrderUrlConfig.getRefundOrderUrl();
		SimpleHttpParam httpParam=new SimpleHttpParam(url);
		httpParam.setMethod(SimpleHttpUtils.HTTP_METHOD_POST);
//		httpParam.addParameter(key, value);
		//
		JSONObject  paramJson=new JSONObject();
		paramJson.put("szCarpassId", carpassId);
		httpParam.addParameter("body", paramJson.toJSONString());
		_log.info("url:"+url);
		_log.info("paramJson:"+paramJson);
		SimpleHttpResult  result=SimpleHttpUtils.httpRequest(httpParam);
		_log.info("result:"+result.getContent());
		int code=result.getStatusCode();
		ResultCreateorder resultCreateorder=new ResultCreateorder();
		if(code!=200){
			resultCreateorder.setCode(code);
			resultCreateorder.setMessage(result.getExceptionMsg());
		}else{
			String content=result.getContent();
			
			JSONObject jsonObj=JSONObject.parseObject(content);
			int jsonCode=jsonObj.getIntValue("code");
			
			JSONObject dataJson=jsonObj.getJSONObject("data");
			String message=dataJson.getString("message");
			
			resultCreateorder.setCode(jsonCode);
			resultCreateorder.setMessage(message);
		}
		
		return resultCreateorder;
	}

}
